import json

from snowflake_id_generator import SnowflakeService


class SnowflakeIdGenerator:
    """
    雪花id生成器
    """

    def __init__(self, worker_id: int = 0, datacenter_id: int = 0):
        self.generator = SnowflakeService(
            worker_id,
            datacenter_id,
            epoch=1288834974657  # hutool的Snowflake的默认起始时间
        )

    def get_id(self) -> int:
        """ 获取snowflake id """
        return self.generator.generate_id()

    def get_id_str(self) -> str:
        """ 获取snowflake id """
        return str(self.get_id())

    def parse_uid(self, s_id: int | str) -> str:
        """ 解析snowflake id """
        if isinstance(s_id, str):
            s_id = int(s_id)

        info: dict = self.generator.parse_id(s_id)
        info["UID"] = s_id
        return json.dumps(info, ensure_ascii=False)


# 初始化雪花id生成器
snowflake_id = SnowflakeIdGenerator()
